var vm = new Vue({
    el: '#app',
    data: {
        host,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        username: "",
        mobile: "",
        email: "",
        email_active: false,
        set_email: false,
        send_email_btn_disabled: false,
        send_email_tip: "重新发送验证邮件",
        email_error: false,
        histories: [] // 记录⽤用户浏览数据
    },
    mounted: function() {
        // 判断用户的登录状态
        if (this.user_id && this.token) {
            axios
                .get(this.host + "/user/", {
                    // 向后端传递JWT token的方法
                    headers: {
                        Authorization: "JWT " + this.token, //jwt后面有空格
                    },
                    responseType: "json",
                })
                .then((response) => {
                    // 加载用户数据
                    this.user_id = response.data.id;
                    this.username = response.data.username;
                    this.mobile = response.data.mobile;
                    this.email = response.data.email;
                    this.email_active = response.data.email_active;
                    // 补充请求浏览历史
                    axios.get(this.host + '/browse_histories/', {
                        headers: {
                            'Authorization': 'JWT ' + this.token
                        },
                        responseType: 'json'
                    })
                    .then(response => {
                        this.histories = response.data;
                        for(var i=0; i<this.histories.length; i++){
                            this.histories[i].url = '/goods/' + this.histories[i].id + '.html';
                        }
                    })
                })
                .catch((error) => {
                    if (error.response.status == 401 || error.response.status == 403) {
                        location.href = "/login.html?next=/user_center_info.html";
                    }
                });
        } else {
            location.href = "/login.html?next=/user_center_info.html";
        }
    },
    methods: {
        // 退出
        logout: function() {
            sessionStorage.clear();
            localStorage.clear();
            location.href = "/login.html";
        },
        // 保存email
        // 保存email
        save_email: function() {
            var re = /^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$/;
            if (re.test(this.email)) {
                this.email_error = false;
            } else {
                this.email_error = true;
                return;
            }
            axios
                .put(
                    this.host + "/email/", { email: this.email }, {
                        headers: {
                            Authorization: "JWT " + this.token,
                        },
                        responseType: "json",
                    }
                )
                .then((response) => {
                    this.set_email = false;
                    this.send_email_btn_disabled = true;
                    this.send_email_tip = "已发送验证邮件";
                })
                .catch((error) => {
                    alert(error.data);
                });
        },
    },
});